import { BaseController } from '#server/common/index.js';

const COOKIE_KEY_TOKEN = 'atomgit_pop_api_token';
const COOKIE_KEY_ATOMGIT_ACCESS_TOKEN = 'ATOMGIT_ACCESS_TOKEN';

class Controller extends BaseController {
  // 首页
  main() {
    // 处理 Token
    let token = this.$ctx.request.cookies[COOKIE_KEY_TOKEN] ?? '';
    // 处理 AtomGit 自动登录
    if (!token) {
      // 获取 AtomGit 的 Token
      token = this.$ctx.request.cookies[COOKIE_KEY_ATOMGIT_ACCESS_TOKEN] ?? '';
      token && this.$ctx.response.cookie(COOKIE_KEY_TOKEN, token, {
        path: '/',
        expires: new Date(Date.now() + 3600000 * 24 * 30),
        sameSite: 'lax',
      });
    }

    this.$ctx.response.set({
      'Content-Type': 'text/html; charset=utf-8',
      'Cache-Control': 'no-cache',
    });
    this.$render('index');
  }
}

export default Controller;
